package agendamento;
import hibernate.dao.HibernateConnection;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.ParseException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import javax.swing.JTable;

import skin.LookAndFeelSelector;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.toedter.calendar.JDateChooser;

import cursor.CursorController;
import factory.FabricaDeFrame;

import javax.swing.JScrollPane;

import java.awt.Color;


public class FrmCadAgendamento extends JFrame {

	private JPanel contentPane;
	private JLabel lblCliente;
	private JLabel lblHorario;
	private JLabel lblData;
	private JLabel lblServico;
	private JTextField txtServico;
	private JComboBox cbListaClientes;
	private JButton btnServico;
	private JSeparator separator;
	private JButton btnAgendar;
	private JButton btnCancelar;
	private JDateChooser dtchDataAgendamento;
	private JComboBox cbHorario;
	private JScrollPane scrollPaneAgendamento;
	private AgendamentoTableModel modeloAgendamento;
	private String [] horarios ={"06:00","06:30","07:00","07:30","08:00","08:30","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30",
			"14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:00",
			"23:30","00:00"};
	private JTable tbAgendamento;
	private CtrlAgendamento ctrl = new CtrlAgendamento();
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HibernateConnection.init();
					LookAndFeelSelector.lookAndFeelOS();
					FrmCadAgendamento frame =  (FrmCadAgendamento) FabricaDeFrame.getInstanceAgendamento();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public FrmCadAgendamento()  {
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Leo Kodama\\Documents\\Eclipse Projects\\TesteparaProjeto\\src\\Repositorio\\icon.png"));
		setTitle("AGENDAMENTO DE HOR\u00C1RIO");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 557, 480);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblCliente = new JLabel("Cliente: ");
		lblCliente.setBounds(30, 14, 68, 14);
		contentPane.add(lblCliente);
		
		cbListaClientes = new JComboBox();
		cbListaClientes.setBounds(108, 11, 319, 27);
		contentPane.add(cbListaClientes);
		ctrl.preencheCB(cbListaClientes);
		
		lblHorario = new JLabel("Hor\u00E1rio: ");
		lblHorario.setBounds(30, 73, 68, 14);
		contentPane.add(lblHorario);
		
		cbHorario = new JComboBox(horarios);
		cbHorario.setBounds(108, 67, 76, 26);
		contentPane.add(cbHorario);
		
		
		lblData = new JLabel("Data: ");
		lblData.setBounds(263, 73, 46, 14);
		contentPane.add(lblData);
		
		dtchDataAgendamento = new JDateChooser();
		dtchDataAgendamento.setBounds(310, 68, 120, 25);
		contentPane.add(dtchDataAgendamento);
		
		
		lblServico = new JLabel("Servi\u00E7o: ");
		lblServico.setBounds(30, 130, 68, 14);
		contentPane.add(lblServico);
		
		txtServico = new JTextField();
		txtServico.setBounds(108, 127, 188, 25);
		contentPane.add(txtServico);
		txtServico.setColumns(10);
		
		btnServico = new JButton("Servi\u00E7os");
		btnServico.setBounds(338, 126, 89, 25);
		ActionListener serv = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				FrmEscolheServico escolheServico = new FrmEscolheServico();
				escolheServico.setVisible(true);
				
			}
		};
		serv = CursorController.createListener(this, serv);
		btnServico.addActionListener(serv);
		contentPane.add(btnServico);
		
		separator = new JSeparator();
		separator.setBounds(10, 174, 571, 6);
		contentPane.add(separator);
		
		btnAgendar = new JButton("Agendar");
		btnAgendar.setBounds(338, 400, 90, 28);
		ActionListener agendar = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ctrl.validaHorario(cbListaClientes, dtchDataAgendamento, cbHorario, txtServico);
			}
		};
		agendar = CursorController.createListener(this, agendar);
		btnAgendar.addActionListener(agendar);
		contentPane.add(btnAgendar);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(434, 400, 90, 28);
		ActionListener cancelar = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//TODO fechar a frame
			}
		};
		cancelar = CursorController.createListener(this, cancelar);
		btnCancelar.addActionListener(cancelar);
		contentPane.add(btnCancelar);
		
		tbAgendamento = new JTable();
		try {
			tbAgendamento.setModel(new AgendamentoTableModel());
			tbAgendamento.getColumnModel().getColumn(0).setPreferredWidth(70);
			tbAgendamento.getColumnModel().getColumn(1).setPreferredWidth(130);
			tbAgendamento.getColumnModel().getColumn(2).setPreferredWidth(130);
			tbAgendamento.getColumnModel().getColumn(3).setPreferredWidth(130);
			tbAgendamento.getColumnModel().getColumn(4).setPreferredWidth(130);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		scrollPaneAgendamento = new JScrollPane();
		scrollPaneAgendamento.setBounds(30, 192, 473, 196);
		scrollPaneAgendamento.setViewportView(tbAgendamento);
		contentPane.add(scrollPaneAgendamento);
		
	}
}
